<template>
  <a-row :gutter="24">
    <a-col :md="24">
      <a-card :style="cardStyle" :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('reportQuery.documentDate')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-range-picker
                    style="width: 100%"
                    v-model="queryParam.createTimeRange"
                    format="YYYY-MM-DD"
                    :placeholder="[$t('reportQuery.startTime'), $t('reportQuery.endTime')]"
                    @change="onDateChange"
                  />
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item :label="$t('manuManagement.documentNo')" :labelCol="labelCol" :wrapperCol="wrapperCol">
                  <a-input :placeholder="$t('manuManagement.pleaseEnterTheDocumentNumber')"
                    v-model="queryParam.number"></a-input>
                </a-form-item>
              </a-col>
              
          <a-col :lg="6" :md="12" :sm="24">
            <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" :label="$t('manuManagement.customer')" data-step="1" :data-title="$t('manuManagement.customer')"
              :data-intro="$t('popup.customerMustUser')">
              <a-select :placeholder="$t('manuManagement.selectCustomer')" v-model="queryParam.organId"
                :dropdownMatchSelectWidth="false" showSearch allowClear optionFilterProp="children">
                <div slot="dropdownRender" slot-scope="menu">
                  <v-nodes :vnodes="menu" />
                  <a-divider style="margin: 4px 0;" />
                </div>
                <a-select-option v-for="(item,index) in cusList" :key="index" :value="item.id">
                  {{ item.supplier }}
                </a-select-option>
              </a-select>
            </a-form-item>
          </a-col>
              <a-col :md="6" :sm="24" >
                <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons table-operator">
                  <a-button type="primary" @click="searchQuery">{{$t('reportQuery.query')}}</a-button>
                  <ColumnsSetting style="ma" @columnsChange="columnsChange" :columns="columns"></ColumnsSetting>
                  <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="reportPrint" icon="printer">{{$t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(5) > -1"  style="margin-left: 8px" @click="generatePrintData" icon="printer">{{$t('inPrint.other') + $t('reportQuery.print')}}</a-button>
                  <a-button v-if="btnEnableList.indexOf(3) > -1" :loading="downLoading" style="margin-left: 8px" @click="exportExcel" icon="download">{{$t('reportQuery.export')}}</a-button>
               
                </span>
              </a-col>
            </a-row>
            <a-row :gutter="24">
              <a-col :lg="6" :md="12" :sm="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol"
                  :label="$t('reportQuery.warehouse')">
                  <a-select :placeholder="$t('reportQuery.pleaseSelectAWarehouse')" allowClear
                    v-model="queryParam.depotId">
                    <a-select-option v-for="(item, index) in warehouseList" :key="index" :value="item.id">
                      {{ item.depotName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- table区域-begin -->
        <div ref="print" id="reportPrint">
          <a-table
            bordered
            ref="table"
            size="middle"
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            @change="handleTableChange">
            <template slot="CustomerOrderNumber" slot-scope="text, record">
              <a @click="myHandleDetail(record, $t('reportQuery.orderCount'))">{{text}}</a>
            </template>
            <template slot="operNumber" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.orderCount'))">{{text}}</a>
            </template>
            <template slot="quotationNumber" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.quotationNumber'))">{{text}}</a>
            </template>
            <template slot="exchangeNumber" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.exchangeNumber'))">{{text}}</a>
            </template>
            <template slot="breakNumber" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.breakNumber'))">{{text}}</a>
            </template>
            <template slot="breakNumber1" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.breakNumber'))">{{text}}</a>
            </template>
            <template slot="finishNumber" slot-scope="text, record">
              <a @click="showPurchaseTrackingInOutList(record, $t('reportQuery.finishNumber'))">{{text}}</a>
            </template>
          </a-table>
          <a-row :gutter="24" style="margin-top: 8px;text-align:right;">
            <a-col :md="24" :sm="24">
              <a-pagination @change="paginationChange" @showSizeChange="paginationShowSizeChange"
                size="small"
                show-size-changer
                :showQuickJumper="true"
                :current="ipagination.current"
                :page-size="ipagination.pageSize"
                :page-size-options="ipagination.pageSizeOptions"
                :total="ipagination.total"
                :show-total="(total, range) => $t('reportQuery.common') +`${total-Math.ceil(total/ipagination.pageSize)}`+ $t('reportQuery.strip')">
                <template slot="buildOptionText" slot-scope="props">
                  <span>{{ props.value-1 }}{{$t('reportQuery.strip')}}/{{$t('reportQuery.page')}}</span>
                </template>
              </a-pagination>
            </a-col>
          </a-row>
        </div>
        <!-- table区域-end -->
        <!-- 表单区域 -->
        <salesTrackingInOutList ref="salesTrackingInOutList"></salesTrackingInOutList>
        <bill-detail ref="modalDetail"></bill-detail>
      </a-card>
    </a-col>
    <saleOutRepotModual ref="saleOutRepotModual" :list="dataSource" :columns="columns" :queryParam="queryParam"></saleOutRepotModual>
  </a-row>
</template>
<script>
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import { getNowFormatYear, openDownloadDialog, sheet2blob} from "@/utils/util"
  import {getAction} from '@/api/manage'
  import {findBySelectSup, findBillDetailByNumber,findBySelectCus} from '@/api/api'
  import JEllipsis from '@/components/jeecg/JEllipsis'
  import salesTrackingInOutList from './modules/salesTrackingInOutList'
  import moment from 'moment'
  import BillDetail from '../bill/dialog/BillDetail'
  import { filterObj } from '@/utils/util'
  import saleOutRepotModual from './modules/saleOutRepotModual';
  export default {
    name: "purchaseOrderTracking",
    mixins:[JeecgListMixin],
    components: {
      JEllipsis,
      salesTrackingInOutList,
      BillDetail,
      VNodes: {
        functional: true,
        render: (h, ctx) => ctx.props.vnodes,
      },
      saleOutRepotModual
    },
    data () {
      return {
        detailType: '',
        labelCol: {
          span: 5
        },
        wrapperCol: {
          span: 18,
          offset: 1
        },
        // 查询条件
        queryParam: {
          organId: '',
            number: '',
            beginTime: getNowFormatYear() + '-01-01',
            endTime: moment().format('YYYY-MM-DD'),
            depotId: '',
          type: '销售订单追踪',
        },
        ipagination:{
          pageSize: 11,
          pageSizeOptions: ['11', '21', '31', '101', '201']
        },
        dateFormat: 'YYYY-MM-DD',
        currentDay: moment().format('YYYY-MM-DD'),
        defaultTimeStr: '',
        supList: [],
        depotList: [],
        warehouseList: [],
        cusList:[],
        tabKey: "1",
        // 表头
        columns: [
          {
            title: this.$t('defaultTranslate.lineNumber'),dataIndex: 'rowIndex',width:'3%', align: 'center',
            customRender: (t,r,index) => parseInt(index)+1
          },
          {title:this.$t('reportQuery.documentDate'), dataIndex: 'operTimeStr',align: 'center', width: 60},
          {title:this.$t('reportQuery.CustomerOrderNumber'),dataIndex: 'number',align: 'center',width: 60, scopedSlots: { customRender: 'CustomerOrderNumber'}},
          {title:this.$t('reportQuery.orderStatus'), dataIndex: 'status',align: 'center', width: 30,
            customRender: (text,record) => {
              switch (record.status) {
                case '0':
                  return (<a-tag color="red">{this.$t('reportQuery.unchecked')}</a-tag>)
                case '1': 
                  return (<a-tag color="green">{this.$t('reportQuery.checked')}</a-tag>)
                case '2': 
                  return (<a-tag color="blue">{this.$t('salesManagement.completeSales')}</a-tag>)
                case '3':
                  return (<a-tag color="cyan">{this.$t('manuManagement.partialSales')}</a-tag>)
              }
            }
          },
          // {title:this.$t('reportQuery.exchangeStatus'), dataIndex: 'exchangeStatus',align: 'center', width: 30,
          //   customRender: (text,record) => {
          //     switch (record.status) {
          //       case '0':
          //         return (<a-tag color="red">{this.$t('reportQuery.exchangeNotCompleted')}</a-tag>)
          //       case '1': 
          //         return (<a-tag color="green">{this.$t('reportQuery.exchanged')}</a-tag>)
          //     }
          //   }
          // },
          {title:this.$t('reportQuery.orderCount'), dataIndex: 'operNumber',align: 'center', width: 15,scopedSlots: { customRender: 'operNumber'}},
          // {title:this.$t('reportQuery.exchangeNumber'), dataIndex: 'exchangeNumber',align: 'center', width: 15,scopedSlots: { customRender: 'exchangeNumber'}},
          {title:this.$t('reportQuery.breakNumber'), dataIndex: 'breakNumber',align: 'center', width: 15,scopedSlots: { customRender: 'breakNumber'}},
          {title:this.$t('reportQuery.advanceCollection'), dataIndex: 'deposit',align: 'center', width: 15},
          {title:this.$t('manuManagement.collection'), dataIndex: 'amount',align: 'center', width: 15},
          {title:this.$t('reportQuery.deliveryQuantity'), dataIndex: 'finishNumber',align: 'center', width: 15,scopedSlots: { customRender: 'finishNumber'}}
        ],
        url: {
          list: "/depotHead/salesOrderTracking",
          detailList: '/depotItem/getDetailList'
        }
      }
    },
    created () {
      this.getDepotData()
      this.initCustomer()
      this.initWarehouseList()
      this.defaultTimeStr = [moment(getNowFormatYear() + '-01-01', this.dateFormat), moment(this.currentDay, this.dateFormat)]
    },
    methods: {
      moment,
      
      getQueryParams() {
        //获取查询条件
        let sqp = {}
        if (this.superQueryParams) {
          sqp['superQueryParams'] = encodeURI(this.superQueryParams)
          sqp['superQueryMatchType'] = this.superQueryMatchType
        }
        let searchObj = {}
        searchObj.search = JSON.stringify(this.queryParam)
        var param = Object.assign(sqp, searchObj, this.isorter, this.filters)
        param.field = this.getQueryField()
        param.currentPage = this.ipagination.current
        param.pageSize = this.ipagination.pageSize
        return filterObj(param)
      },
      onDateChange: function (value, dateString) {
        console.log(dateString[0],dateString[1]);
        this.queryParam.beginTime=dateString[0];
        this.queryParam.endTime=dateString[1];
      },
    initCustomer () {
      let that = this
      findBySelectCus({}).then(res => {
        if (res) {
          that.cusList = res
        }
      })
    },
      getDepotData() {
        getAction('/depot/findDepotByCurrentUser').then((res)=>{
          if(res.code === 200){
            this.depotList = res.data;
          }else{
            this.$message.info(res.data);
          }
        })
      },
      myHandleDetail(record) {
        findBillDetailByNumber({ number: record.number }).then((res) => {
          if (res && res.code === 200) {
            /**
             * @param1 dataSource
             * @param2 title billDetail根据title展示哪种表格
             */
            this.handleDetail(res.data, res.data.subType);
          }
        })
      },
      searchQuery() {
        if(this.queryParam.beginTime == '' || this.queryParam.endTime == ''){
          this.$message.warning(this.$t('reportQuery.pleaseDate'))
        } else {
          this.loadData(1);
        }
      },
      // 获取仓库
    initWarehouseList () {
      getAction('/depot/findDepotByCurrentUser').then(res => {
        if (res.data) {
          this.warehouseList = res.data
        }
      })
    },
    loadData (arg) {
      console.log(this.url.list)
      if (!this.url.list) {
        this.$message.error('请设置url.list属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      this.loading = true
      getAction(this.url.list, params).then(res => {
        if (res.code === 200) {
          this.dataSource = res.data.rows
          this.ipagination.total = res.data.total
        }
        if (res.code === 510) {
          this.$message.warning(res.data)
        }
        this.loading = false
      })
    },
    showPurchaseTrackingInOutList(record, title) {
      this.$refs.salesTrackingInOutList.show(record, title);
      this.$refs.salesTrackingInOutList.disableSubmit = false;
    },
    
        
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>